package net.pessu.android.shoppoid.debrief;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;

import java.util.List;

import net.pessu.android.shoppoid.R;
import net.pessu.android.shoppoid.bean.Article;
import net.pessu.android.shoppoid.gui.IhmObjets;
import net.pessu.android.shoppoid.gui.ListArticlesBill;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class Debrief extends GDActivity {

	private static final String IL_MANQUE_LA_REDUCTION_D_UN_OU_PLUSIEURS_ARTICLES = IhmObjets
			.getStringContext(R.string.manquereduc);
	private static final String IL_MANQUE_LE_PRIX_D_UN_OU_PLUSIEURS_ARTICLES = IhmObjets
			.getStringContext(R.string.manqueprix);
	private static final String ATTENTION = IhmObjets
			.getStringContext(R.string.attention);
	private static final String TOTAL_ECONOMISE = IhmObjets
			.getStringContext(R.string.totaleco);
	private static final String TOTAL = IhmObjets
			.getStringContext(R.string.total);
	DebriefService service;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.debriefing);
		ActionBar bar = getActionBar();
		bar.setTitle(getString(R.string.ticket));
		bar.setFont(IhmObjets.tfTitre);
		bar.setFontSize(30);
		service = DebriefService.getInstance(this);

		ListArticlesBill maListViewPerso = (ListArticlesBill) findViewById(R.id.listviewdebriefing);
		maListViewPerso.init(this);

		double totalPaye = 0.0;
		double totalGagne = 0.0;
		boolean missPrix = false;
		boolean missReduc = false;

		List<Article> list = service.getListArticlesAchetes();
		for (Article article : list) {
			totalPaye += article.getPrixreduit();
			if (article.getTauxReduc() != 0) {
				totalGagne += (article.getPrixreduit() / article.getTauxReduc())*(1-article.getTauxReduc());
			} else {
				missReduc = true;
			}
			if (article.getPrixreduit() == 0) {
				missPrix = true;
			}
		}

		TextView footerTotal = new TextView(this);
		footerTotal.setGravity(Gravity.RIGHT);
		footerTotal.setTextColor(Color.BLACK);
		maListViewPerso.setFooterDividersEnabled(false);
		StringBuilder message = new StringBuilder();
		message.append(TOTAL);
		message.append(IhmObjets.df.format(totalPaye));
		if (totalGagne > 0) {
			message.append(TOTAL_ECONOMISE);
			message.append(IhmObjets.df.format(totalGagne));
		}
		footerTotal.setTypeface(IhmObjets.tf);
		footerTotal.setText(message.toString());

		maListViewPerso.addFooterView(footerTotal);

		if (missPrix || missReduc) {
			TextView footerAvertissement = new TextView(this);
			footerAvertissement.setGravity(Gravity.RIGHT);
			footerAvertissement.setTextColor(Color.RED);
			StringBuilder avertissement = new StringBuilder();
			avertissement.append(ATTENTION);
			if (missPrix) {
				avertissement
						.append(IL_MANQUE_LE_PRIX_D_UN_OU_PLUSIEURS_ARTICLES);
			}
			// TODO: gérer les pluriels
			if (missReduc) {
				avertissement
						.append(IL_MANQUE_LA_REDUCTION_D_UN_OU_PLUSIEURS_ARTICLES);
			}
			footerAvertissement.setText(avertissement.toString());
			maListViewPerso.addFooterView(footerAvertissement);
		}

		maListViewPerso.setList(service.getListArticlesAcheter());
		// maListViewPerso.setCursor(service.getListArticlesAchetesC());

	}
}
